﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUS;
using DTO;

namespace GUI
{
    public partial class frmPhieuNhapKho : Form
    {
        public frmPhieuNhapKho()
        {
            InitializeComponent();
        }

        private void frmPhieuNhapKho_Load(object sender, EventArgs e)
        {
            HienThiDanhSach();
        }

        public void HienThiDanhSach()
        {
            PhieuNhapKhoBUS pnkBUS = new PhieuNhapKhoBUS();
            List<PhieuNhapKhoDTO> listPNK = new List<PhieuNhapKhoDTO>();
            PhieuNhapKhoDTO pnkDTO = new PhieuNhapKhoDTO();
            try
            {
                listPNK = pnkBUS.LayDanhSachPhieuNhapKho();
                this.dgvDanhSach.Rows.Clear();
                for (int i = 0; i < listPNK.Count; i++)
                {
                    pnkDTO = listPNK[i];
                    String MaPhieuNK = pnkDTO.MaPhieuNK.ToString();
                    String NgayLap = pnkDTO.NgayLap.ToString();
                    String MaNV = pnkDTO.MaNV.ToString();
                    String MaDDH = pnkDTO.MaDDH.ToString();
                    String TongTien = pnkDTO.TongTien.ToString();
                    String TenNV = pnkDTO.TenNV.ToString();
                    dgvDanhSach.Rows.Add(MaPhieuNK, NgayLap, MaNV, TenNV, MaDDH, TongTien);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi:" + ex.ToString());
            }
        }

        public void HienThiChiTiet(int ma)
        {
            CT_PhieuNhapKhoBUS ctpnkBUS = new CT_PhieuNhapKhoBUS();
            List<CT_PhieuNhapKhoDTO> listPNK = new List<CT_PhieuNhapKhoDTO>();
            try
            {
                listPNK = ctpnkBUS.LayChiTietPhieuNhapKhoTheoMa(ma);
                this.dgvChiTiet.Rows.Clear();

                for (int i = 0; i < listPNK.Count; i++)
                {
                    CT_PhieuNhapKhoDTO ctpnkDTO = new CT_PhieuNhapKhoDTO();
                    ctpnkDTO = listPNK[i];

                    String MaNVL = ctpnkDTO.MaNVL.ToString();
                    String SoLuong = ctpnkDTO.SoLuong.ToString();
                    String DonGiaNhap = ctpnkDTO.DonGiaNhap.ToString();
                    String Ten = ctpnkDTO.Ten.ToString();
                    String DonViTinh = ctpnkDTO.DonViTinh.ToString();

                    dgvChiTiet.Rows.Add(MaNVL, Ten, SoLuong, DonViTinh, DonGiaNhap);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi dữ liệu");
                MessageBox.Show(ex.ToString());
            }
        }

        private void dgvDanhSach_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
            {
                return;
            }
            int intIndex = e.RowIndex;
            dgvDanhSach.Rows[intIndex].Selected = true;

            int dong = dgvDanhSach.SelectedCells.Count;
            if (dong == 0)
                return;
            dong = dgvDanhSach.SelectedCells[0].RowIndex;
            try
            {
                int ma = int.Parse(dgvDanhSach.Rows[dong].Cells[0].Value.ToString());
                HienThiChiTiet(ma);
            }
            catch
            {
                return;
            }
        }

        private void buttonX4_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            frmThemPhieuNhapKho fr = new frmThemPhieuNhapKho();
            fr.ShowDialog();
        }
    }
}
